package com.mxpio.erp.inventory.service.impl;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mxpio.erp.common.inventory.entity.VInvLotStock;
import com.mxpio.erp.common.inventory.service.VInvLotStockService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.lin.Linq;
import com.mxpioframework.jpa.query.Criteria;

@Service
public class VInvLotStockServiceImpl implements VInvLotStockService {

	@Override
	@Transactional(readOnly = true)
	public Page<VInvLotStock> listPage(Pageable pageAble, Criteria c) {
		Linq linq =JpaUtil.linq(VInvLotStock.class);
		// @formatter:off
		return linq.select("itemCode","whCode","itemName","itemGroupCode","itemSpec","unitCode","drawingNo","imgFile","whType",linq.criteriaBuilder().sum(linq.root().get("quantity")).alias("quantity"),linq.criteriaBuilder().sum(linq.root().get("fee")).alias("fee"),linq.criteriaBuilder().sum(linq.root().get("borrowedQuantity")).alias("borrowedQuantity"))
			.where(c)
			.groupBy("itemCode","whCode","itemName","itemGroupCode","itemSpec","unitCode","drawingNo","imgFile","whType")
			.paging(pageAble);
		// @formatter:on
	}

	@Override
	@Transactional(readOnly = true)
	public List<VInvLotStock> list(Criteria c) {
		Linq linq =JpaUtil.linq(VInvLotStock.class);
		// @formatter:off
		return linq.select("itemCode","whCode","itemName","itemGroupCode","itemSpec","unitCode","drawingNo","whType",linq.criteriaBuilder().sum(linq.root().get("quantity")).alias("quantity"),linq.criteriaBuilder().sum(linq.root().get("fee")).alias("fee"),linq.criteriaBuilder().sum(linq.root().get("borrowedQuantity")).alias("borrowedQuantity"))
				.where(c)
				.groupBy("itemCode","whCode","itemName","itemGroupCode","itemSpec","unitCode","drawingNo","whType")
				.list();
		// @formatter:on
	}
	
	@Override
	@Transactional(readOnly = true)
	public Page<VInvLotStock> listPageByLot(Pageable pageAble, Criteria c) {
		return JpaUtil.linq(VInvLotStock.class).where(c).paging(pageAble);
	}

	@Override
	@Transactional(readOnly = true)
	public List<VInvLotStock> listByLot(Criteria c) {
		return JpaUtil.linq(VInvLotStock.class).where(c).list();
	}

	@Override
	@Transactional(readOnly = true)
	public VInvLotStock listByLotSumFee(Criteria c) {//根据条件汇总fee
		Linq linq =JpaUtil.linq(VInvLotStock.class);
		return linq.select(linq.criteriaBuilder().sum(linq.root().get("fee")).alias("fee"))
				.where(c)
				.findOne();
	}


}
